package com.xnfh.equipment.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xnfh.common.bean.EquipmentToken
import com.xnfh.common.bean.WarningBean
import com.xnfh.equipment.R
import com.xnfh.equipment.adapter.SystemLogAdapter
import com.xnfh.equipment.databinding.FragmentEquipmentLogLayoutBinding
import com.xnfh.equipment.viewmodel.EquipmentLogViewModel
import com.xnfh.framework.base.BaseFragment
import com.xnfh.framework.base.BaseStateObserver
import com.xnfh.network.constant.SFJ_API_TOKEN
import com.xnfh.network.constant.SFJ_TOKEN_V2

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/12/27 14:02
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/12/27 14:02
 * @UpdateRemark: 更新说明
 */
class EquipmentSystemLogFragment:BaseFragment<FragmentEquipmentLogLayoutBinding>() {
    override fun getLayoutID(): Int = R.layout.fragment_equipment_log_layout
    companion object {
        fun getInstance(bundle: Bundle? = null): EquipmentSystemLogFragment {
            val plantOptionFragment = EquipmentSystemLogFragment()
            if (bundle != null) {
                plantOptionFragment.arguments = bundle
            }
            return plantOptionFragment
        }
    }
    private var viewModel :EquipmentLogViewModel? = null
    private var page = 1
    private var gateWayId: String? = null
    private var adapter : SystemLogAdapter? = null


    override fun initView(view: View, savedInstanceState: Bundle?) {
        gateWayId = arguments?.getString("id")
        mBind.logRecycle.layoutManager = LinearLayoutManager(requireContext())
        adapter = SystemLogAdapter()
        mBind.logRecycle.adapter = adapter
        mBind.logRefresh.autoRefresh()
        mBind.logRefresh.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel?.requestKcTokenV2()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel?.requestKcTokenV2()
            }
        })
    }

    override fun observe() {
        viewModel = EquipmentLogViewModel()
        viewModel?.tokenV2?.observe(this,object :BaseStateObserver<EquipmentToken>(){
            override fun getRespDataSuccess(it: EquipmentToken) {
                super.getRespDataSuccess(it)
                SFJ_API_TOKEN = it.apiToken
                SFJ_TOKEN_V2 = it.TokenV2
                viewModel?.requestLogList(gateWayId, page)
            }

            override fun getRespDataError() {
                super.getRespDataError()
                mBind.logRefresh.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
            }
        })
        viewModel?.warningBean?.observe(this,object : BaseStateObserver<WarningBean>(){
            override fun getRespDataSuccess(it: WarningBean) {
                super.getRespDataSuccess(it)
                mBind.logRefresh.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
                if(page == 1 && it.Data.isEmpty()){
                    adapter?.setEmptyView(com.xnfh.common.R.layout.adapter_empty_view)
                    return
                }
                if(page == 1) {
                    adapter?.setNewInstance(it.Data.toMutableList())
                }else{
                    adapter?.addData(it.Data)
                }
            }

            override fun getRespDataError() {
                super.getRespDataError()
                mBind.logRefresh.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
            }
        })
    }
}